package com.wing.cms.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wing.cms.domain.Channel;
import com.wing.cms.domain.Site;
import com.wing.cms.service.ChannelManager;
import com.wing.cms.service.SiteManager;
import com.wing.common.Constants;

/**
 * 
 * 
 * @author please  write your big name
 * 
 */
 
@Controller
@RequestMapping(value = "/")
public class FrontSiteController {

	@Autowired
	private SiteManager siteManager;
	@Autowired
	private ChannelManager channelManager;
	

	/**
	 * 获得列表数据
	 * @throws IOException 
	 * @throws ServletException 
	 *  
	 */
	@RequestMapping(value = "/{file}")
	public String list(@PathVariable String file,HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String url = request.getServerName();
		String sitepath = (String)Constants.urlMap.get(url);
		if(sitepath!=null){
			request.getRequestDispatcher("/site/"+sitepath+"/"+file+".jsp").forward(request, response);
		}
		return null;
		
	}
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String first(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String url = request.getServerName();
		String sitepath = (String)Constants.urlMap.get(url);
		
		
		if(sitepath!=null){
			//System.out.println("+++++++++++++++"+sitepath+"++++++++++++++"+(String)Constants.urlMap.get(url+"_status")+"+++++++++");
			String redircturl=(String)Constants.urlMap.get(url+"_homeurl");
			request.getRequestDispatcher(request.getRequestURI()+redircturl).forward(request, response);
//			if(Site.SITE_STATUS_DIRECT.equals((String)Constants.urlMap.get(url+"_status"))){
//				request.getRequestDispatcher("/site/"+sitepath+"/index.jsp").forward(request, response);
//			}else{
//				List list = channelManager.findChannelsBySiteId( (String)Constants.urlMap.get(url+"_id"));
//				String redircturl = "/site/"+sitepath+"/index.jsp";
//				System.out.println("+++++++++++++++"+list.size()+"++"+Channel.CHANNELROOT+"++++++"+(String)Constants.urlMap.get(url+"_id")+"+++++++++++++");
//				if(list.size()>0){
//					Map map = (Map)list.get(0);
//					redircturl = "/cms/channel/layout/"+map.get("id");
//				}
//				//response.sendRedirect(redircturl);
//				request.getRequestDispatcher(request.getRequestURI()+redircturl).forward(request, response);
//			}
//			response.setCharacterEncoding(code);
			//System.out.println("+++++++++++++++++frontsitecontroller:"+(new Date().getTime())+"++++++++++++++++++++++++++++");
			return null;
		}
		
		return null;
		
	}
}

